package cn.cjj.gulimall.cart.controller;

import cn.cjj.common.constant.AuthServerConstant;
import cn.cjj.gulimall.cart.interceptor.CartInterceptor;
import cn.cjj.gulimall.cart.service.CartService;
import cn.cjj.gulimall.cart.vo.Cart;
import cn.cjj.gulimall.cart.vo.CartItem;
import cn.cjj.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author CuiJunJie
 * @create 2021/7/6-15:31
 */
@Controller
public class CartController {

    @Autowired
    CartService cartService;


    @ResponseBody
    @RequestMapping("/currentUserCartItems")
    public List<CartItem> currentUserCartItems() {
        return cartService.currentUserCartItems();
    }


    @RequestMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);

        return "redirect:http://cart.gulimall.com/cart.html";
    }


    @RequestMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.countItem(skuId, num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }


    @RequestMapping("/checkCart")
    public String checkCart(@RequestParam("skuId") Long skuId, @RequestParam("isChecked") Integer isChecked) {
        cartService.checkItem(skuId, isChecked);

        return "redirect:http://cart.gulimall.com/cart.html";
    }


    @RequestMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
                              RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {

        CartItem cartItem = cartService.addToCart(skuId, num);

        redirectAttributes.addAttribute("skuId", skuId);

        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @RequestMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {

        CartItem cartItem = cartService.getCartItem(skuId);

        model.addAttribute("cartItem", cartItem);

        return "success";
    }


    @RequestMapping("/cart.html")
    private String cartListPage(HttpSession session, Model model) throws ExecutionException, InterruptedException {

        Cart cart = cartService.getCart();

        model.addAttribute("cart", cart);

        return "cartList";
    }


    @Value("${module.name}")
    private String moduleName;

    @RequestMapping("/module/name")
    @ResponseBody
    public String moduleName() {
        return moduleName;
    }
}
